package com.hfzy.ihk.common.core.elasticsearch.base;

import com.hfzy.ihk.common.core.elasticsearch.ElasticsearchOperation;
import org.elasticsearch.common.UUIDs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/18.
 *
 * @author wws
 */
public class BestDoContextHolder {

    private static final Logger logger = LoggerFactory.getLogger(BestDoContextHolder.class);

    /**
     * 操作
     * */
    private static ThreadLocal<List<ElasticsearchOperation>> listOperation = ThreadLocal.withInitial(() -> new ArrayList<>());

    private static ThreadLocal<String> transactionId = new ThreadLocal<>();


    /**
     * 要发送的消息
     *
     * //TODO 不论是否异步发送，必须要有消息确认机制才能保证最终一致
     *
     * */
    private static ThreadLocal<List<String>> listJsonMessage = ThreadLocal.withInitial(() -> new ArrayList<>());

/********************************一般操作****************************************/
    public static void addOperation(ElasticsearchOperation operation){

        if(operation == null) {
            return;
        }

        List<ElasticsearchOperation> elasticsearchOperationList = listOperation.get();

        if(elasticsearchOperationList == null){
            elasticsearchOperationList = new ArrayList<>();
            listOperation.set(elasticsearchOperationList);
        }

        elasticsearchOperationList.add(operation);

    }

    public static void removeOperation(ElasticsearchOperation operation){

        List<ElasticsearchOperation> elasticsearchOperationList = listOperation.get();

        if(elasticsearchOperationList == null){
           return;
        }

        elasticsearchOperationList.remove(operation);
    }

    public static void clearOperation(){

        List<ElasticsearchOperation> elasticsearchOperationList = listOperation.get();

        if(elasticsearchOperationList == null){
            return;
        }

        elasticsearchOperationList.clear();
    }

    public static List<ElasticsearchOperation> allOperation() {

        return listOperation.get();
    }
    /**************************事务ID*********************************/
    public static String getTransactionId(){

        String txId = transactionId.get();

        if(txId != null){
            return txId;
        }

        transactionId.set(UUIDs.base64UUID());

        return transactionId.get();
    }
    public static void clearTransactionId(){

        transactionId.remove();

    }

    /**************************消息*********************************/

    public static void addJsonMessage(String jsonMessage){

        if(jsonMessage == null) {
            return;
        }

        List<String> stringList = listJsonMessage.get();

        if(stringList == null){
            stringList = new ArrayList<>();
            listJsonMessage.set(stringList);
        }

        stringList.add(jsonMessage);

    }

    public static void removeJsonMessage(String jsonMessage){

        List<String> stringList = listJsonMessage.get();

        if(stringList == null){
            return;
        }

        stringList.remove(jsonMessage);
    }

    public static void clearMessage(){

        List<String> stringList = listJsonMessage.get();

        if(stringList == null){
            return;
        }

        stringList.clear();
    }

    public static List<String> allMessage() {

        return listJsonMessage.get();
    }
}
